/**
 * @file     maxSatisfied.ts
 * @brief    [1052. 爱生气的书店老板](https://leetcode.cn/problems/grumpy-bookstore-owner/)
 * @author   Zhu
 * @date     2024-11-21 19:27
 */

function maxSatisfied(
    customers: number[],
    grumpy: number[],
    minutes: number
): number {
    const n = customers.length;
    let i = 0,
        gain = 0, // 增益，只有在grumpy的时候是有增益的；不生气的时候没有收益
        max = 0; // 最大增益
    const sum = customers.reduce((a, c, j) => {
        a += grumpy[j] ? 0 : c;
        return a;
    }, 0);

    const getGrumpyCustomers = (k) => (grumpy[k] ? customers[k] : 0);

    while (i < n) {
        gain +=
            getGrumpyCustomers(i) -
            (i < minutes ? 0 : getGrumpyCustomers(i - minutes));
        if (gain > max) max = gain;
        i++;
    }

    return sum + max; // 常规客流量 + 增益流量
}
